--  MicroHUD is an original HUD by Shane :
--   http://www.facepunch.com/showthread.php?p=2120729
-- It was rewritten and adapted to Inscript by Ha3
-- with original author's permission.

PLUGIN.Name = "Shane's MicroHUD"
PLUGIN.DefaultOn = false
PLUGIN.Description = 
[[MicroHUD by Shane, now as an Inscript little plugin.

Pretty much the slimmest, tidiest HUD out there.
It is great for people who don't pay a lot of attention to their information but like to check every once in awhile. It features a clock which is exactly what your computers time is for easy access.

Original creation by Shane as an independant addon, rewritten and adapted by Ha3.]]
PLUGIN.Author = "Shane"
PLUGIN.Trigger = false


function PLUGIN:LoadParameters()
	self:AddParameter("x_relpos",    { Type = "slider" , Text = "Grid X position", Defaults = "0", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("y_relpos",    { Type = "slider" , Text = "Grid Y position", Defaults = "16", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("padding",      { Type = "slider"   , Text = "Padding", Defaults = "4", Min = "0", Max = "8", Decimals = "0" } )
	
	self:AddParameter("useclock",    { Type = "checkbox" , Text = "Enable Clock", Defaults = "1" } )
	self:AddParameter("clockformat", { Type = "checkbox" , Text = "Use 24-h format", Defaults = "0" } )
end

function PLUGIN:Load()
	self.SID_Clock  = surface.GetTextureID( "shane/hud/clock" )
	self.SID_Heart  = surface.GetTextureID( "shane/hud/heart" )
	self.SID_Shield = surface.GetTextureID( "shane/hud/shield" )
	self.CO_Main   =  Color( 0, 0, 0, 128 )
	self.CO_Text   =  Color( 255, 255, 255, 255 )
end

function PLUGIN:Unload()
end

function PLUGIN.HOOK:HUDShouldDraw( name )
	if name == "CHudHealth"        then return false end
	if name == "CHudBattery"       then return false end
	if name == "CHudAmmo"          then return false end
	if name == "CHudSecondaryAmmo" then return false end
end

function PLUGIN.HOOK:HUDPaint()
	local ply = LocalPlayer()
	if not (ValidEntity(ply) and ply:Alive()) then return end
	
	local padding = self:GetNumber("padding")
	local X_Ref = padding + self:GetNumber("x_relpos") * ((ScrW() - 2 * padding - 160) / 16 )
	local Y_Ref = padding + self:GetNumber("y_relpos") * ((ScrH() - 2 * padding - 62 ) / 16 )
	
	local useclock = self:GetNumber("useclock") > 0
	local usearmor = ply:Armor() > 0
	
	local myNumberOfPanels = 1 + (useclock and 1 or 0) + (usearmor and 1 or 0)
	local myYMain = Y_Ref + ((self:GetNumber("y_relpos") >= 8) and (23 * (3 - myNumberOfPanels)) or 0)
	
	surface.SetDrawColor( self.CO_Main.r , self.CO_Main.g, self.CO_Main.b, self.CO_Main.a )
	surface.DrawRect( X_Ref     , myYMain,   20,  23 * myNumberOfPanels - 6 ) -- Icons
	surface.DrawRect( X_Ref + 22, myYMain,   78,  23 * myNumberOfPanels - 6 ) -- Main area
	
	-- Draw my individual Inner Backgrounds
	for i = 1, myNumberOfPanels do
		surface.DrawRect( X_Ref + 22, myYMain + (i - 1) * 23, 78, 17 )
	end
	
	-- Draw my Icons and Text
	surface.SetDrawColor( 255, 255, 255, 255 )
	if useclock then
		local sTime = (self:GetNumber("clockformat") > 0) and os.date("%H:%M") or os.date("%I:%M %p")
		
		surface.SetTexture( self.SID_Clock )
		surface.DrawTexturedRect( X_Ref + 2, myYMain + 1, 16, 16 )
		draw.SimpleText( sTime                  , "ScoreboardText", X_Ref + 27, myYMain , self.CO_Text )
	end
	do
		surface.SetTexture( self.SID_Heart )
		surface.DrawTexturedRect( X_Ref + 2, myYMain  + (useclock and 24 or 1), 16, 16 )
		draw.SimpleText( LocalPlayer():Health() , "ScoreboardText", X_Ref + 27, myYMain + (useclock and 23 or 0), self.CO_Text )
	end
	if usearmor then
		surface.SetTexture( self.SID_Shield )
		surface.DrawTexturedRect( X_Ref + 2, myYMain  + (useclock and 47 or 24), 16, 16 )
		draw.SimpleText( LocalPlayer():Armor()  , "ScoreboardText", X_Ref + 27, myYMain + (useclock and 46 or 23), self.CO_Text )
	end
	
	local weapon = ply:GetActiveWeapon()
	if ValidEntity( weapon ) then
		if (ply:GetAmmoCount( weapon:GetPrimaryAmmoType() ) > 0) or (ply:GetAmmoCount( weapon:Clip1() ) > 0) then
			local Slot1 = ( ply:GetAmmoCount( weapon:GetPrimaryAmmoType() ) + weapon:Clip1() )
			local Slot2 = ( ply:GetAmmoCount( weapon:GetSecondaryAmmoType() ) + weapon:Clip2() )
			
			local myYAmmo = Y_Ref + ((self:GetNumber("y_relpos") >= 8) and 23 or 0)
			
			if Slot2 ~= -1 then
				surface.SetDrawColor( self.CO_Main.r , self.CO_Main.g, self.CO_Main.b, self.CO_Main.a )
				surface.DrawRect( X_Ref + 105, myYAmmo, 20, 40 )	 -- Icons	
				surface.DrawRect( X_Ref + 127, myYAmmo, 33, 40 )  -- Main area
				
				surface.DrawRect( X_Ref + 127, myYAmmo, 33, 17 )  	  -- Inner 1
				surface.DrawRect( X_Ref + 127, myYAmmo + 23, 33, 17 )  -- Inner 2
			
				draw.SimpleText( "1"                  , "ScoreboardText", X_Ref + 116, myYAmmo, self.CO_Text, TEXT_ALIGN_CENTER )
				draw.SimpleText( tostring( Slot1 + 1 ), "ScoreboardText", X_Ref + 144, myYAmmo, self.CO_Text, TEXT_ALIGN_CENTER )
				
				draw.SimpleText( "2"                  , "ScoreboardText", X_Ref + 116, myYAmmo + 23, self.CO_Text, TEXT_ALIGN_CENTER )
				draw.SimpleText( tostring( Slot2 + 1 ), "ScoreboardText", X_Ref + 144, myYAmmo + 23, self.CO_Text, TEXT_ALIGN_CENTER )
			
			elseif Slot1 ~= -1 then
				myYAmmo = myYAmmo + (((self:GetNumber("y_relpos") >= 8) and (myNumberOfPanels == 1)) and 23 or 0)
				
				surface.SetDrawColor( self.CO_Main.r , self.CO_Main.g, self.CO_Main.b, self.CO_Main.a )
				surface.DrawRect( X_Ref + 105, myYAmmo, 20, 17 )	 -- Icons	
				surface.DrawRect( X_Ref + 127, myYAmmo, 33, 17 )  -- Main area
				
				surface.DrawRect( X_Ref + 127, myYAmmo, 33, 17 )  -- Inner
			
				draw.SimpleText( "1"                  , "ScoreboardText", X_Ref + 116, myYAmmo, self.CO_Text, TEXT_ALIGN_CENTER )
				draw.SimpleText( tostring( Slot1 + 1 ), "ScoreboardText", X_Ref + 144, myYAmmo, self.CO_Text, TEXT_ALIGN_CENTER )
		
			end
		
		end
		
	end

end

